/** Response */
package domain.session;

import java.text.ParseException;
import java.util.logging.Logger;

import org.json.JSONObject;

/**
 * Response to pick up by sending a request
 * @author jessienewman
 * @version $Revision: 1.0 $
 */
public class Response {
	/**
	 * Field LOGGER.
	 */
	private static final Logger LOGGER = Logger.getLogger(Response.class.getName());
	
	/**
	 * Field success.
	 */
	private boolean success = false;

	/**
	 * Field response.
	 */
	private JSONObject responseJSON; // $codepro.audit.disable variableShouldBeFinal
	
	/**
	 * Field json.
	 */
	private JSONObject json; // $codepro.audit.disable variableShouldBeFinal
	
	/**
	 * Constructor for Response.
	 */
	public Response() {
		responseJSON = new JSONObject();
		json = new JSONObject();
	}
	
	/**
	 * Constructor for Response.
	 * @param response JSONObject
	 */
	public Response(final JSONObject response) {
		try {
			LOGGER.info(response.toString());
			json = new JSONObject(response.toString());
			success = Boolean.TRUE.equals(response.get("success"));
			response.remove("success");
			this.responseJSON = response;
		} catch (final ParseException ex) {
			LOGGER.warning("Parse Exception" + ex);
		}
	}
	
	/**
	 * Constructor for Response.
	 * @param success boolean
	 * @param response JSONObject
	 */
	public Response(boolean success, JSONObject response) {
		this.success = success;
		this.responseJSON = response;
		
		try {
			json = new JSONObject(response.toString());
			json.put("success", success);
		} catch (final ParseException ex) {
			LOGGER.warning("parse exception" + ex);
		}
	}
	
	public void setJSON(final JSONObject json) {
		this.json = json;
		this.json.put("success", this.success);
	}
	
	/**
	 * Method getJSON.
	
	 * @return JSONObject */
	public JSONObject getJSON() {
		return json;
	}
	
	
	public void setSuccessful(final boolean success) {
		this.success = success;
		this.json.put("success", this.success);
	}
	/**
	 * Method successful.
	
	 * @return boolean */
	public boolean isSuccessful() {
		return success;
	}
	
	/**
	 * Method getResponse.
	
	 * @return JSONObject */
	public JSONObject getResponse() {
		return responseJSON;
	}
	
	/**
	 * Method toString.
	
	 * @return String */
	public String toString() {
		return getJSON().toString();
	}
}
